package com.ellricky.baselib.ui.course;


import android.content.Intent;
import android.view.View;

import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.dialog.picker.EPickerEntityListener;
import com.ellricky.baselib.dialog.picker.EPickerUtil;
import com.ellricky.baselib.entity.IdNamePickerEntity;
import com.ellricky.baselib.entity.course.CourseFileEntity;
import com.ellricky.baselib.widget.EditItemView;
import com.flyco.roundview.RoundTextView;

public class CourseFileAddActivity extends BaseToolBarActivity<Void, BasePresenter<Void>> {


    private EditItemView etHourNum;
    private EditItemView etFile;
    private EditItemView etCourseType;
    private RoundTextView btnSubmit;


    private EPickerUtil ePickerUtil;
    private IdNamePickerEntity selFile;
    private IdNamePickerEntity selCouseType;

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_course_add_file;
    }

    @Override
    protected void initUIView() {
        super.initUIView();
        mTvTitle.setText(R.string.base_add_file);

        etHourNum = findViewById(R.id.etHourNum);
        etCourseType = findViewById(R.id.etCourseType);
        etFile = findViewById(R.id.etFile);
        btnSubmit = findViewById(R.id.btnSubmit);

        ePickerUtil = new EPickerUtil(this);

        etCourseType.setOnClickListener(this);
        etFile.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);

    }

    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == etCourseType) {
            ePickerUtil.checkCourseTypePicker(selCouseType, new EPickerEntityListener<IdNamePickerEntity>() {
                @Override
                public void onSelectItem(int pickType, IdNamePickerEntity t) {
                    selCouseType = t;
                    etCourseType.setValue(t.getName());
                }
            });
            return;
        }
        if (v == etFile) {
            String courseType = etCourseType.getValue();
            if (EmptyUtil.isEmpty(courseType)) {
                ToastUtil.show(this, R.string.base_course_type_select);
                return;
            }
            ePickerUtil.checkFilePicker(selFile, selCouseType.getId(), new EPickerEntityListener<IdNamePickerEntity>() {
                @Override
                public void onSelectItem(int pickType, IdNamePickerEntity t) {
                    selFile = t;
                    etFile.setValue(t.getName());
                }
            });
            return;
        }
        if (v == btnSubmit) postSubmit();
    }


    private void postSubmit() {
        String classNum = etHourNum.getValue();
        if (EmptyUtil.isEmpty(classNum)) {
            ToastUtil.show(this, R.string.base_input_hour);
            return;
        }

        String courseType = etCourseType.getValue();
        if (EmptyUtil.isEmpty(courseType)) {
            ToastUtil.show(this, R.string.base_course_type_select);
            return;
        }

        String file = etFile.getValue();
        if (EmptyUtil.isEmpty(file)) {
            ToastUtil.show(this, R.string.base_select_file);
            return;
        }

        // 直接传递回去
        CourseFileEntity fileEntity = new CourseFileEntity(
                classNum, selFile.getId(), selFile.getName()
        );

        Intent intent = new Intent();
        intent.putExtra("file",fileEntity);
        setResult(BaseConstant.RESULT_CODE_ADD,intent);
        finish();


       /* Map<String, Object> paramMap = new HashMap<>();
        String url = EAppUtil.getApiHost();
        HttpUtils.getInstance().post(url, paramMap, new JOCallback(this) {
            @Override
            protected void onSuccess(JSONObject jsonObject, String s) {
                setResult(BaseConstant.RESULT_CODE_ADD);
                finish();
            }
        });*/

    }
}

